package com.hs.ph.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.hs.ph.dao.CommunityDao;
import com.hs.ph.dao.impl.CommunityDaoImpl;
import com.hs.ph.entity.Community;
import com.hs.ph.utitl.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String communityName = req.getParameter("communityName");
        String action = req.getParameter("action");
        CommunityDao communityDao = new CommunityDaoImpl();
        PrintWriter writer = resp.getWriter();

        Map<String,String> map = new HashMap<>();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("communityName",communityName);

        if("selectAll".equals(action)){
            List<Community> list = communityDao.selectAll(map);
            int total = communityDao.selectCount(map);
            Map resMap=new HashMap();
            resMap.put("list",list);
            resMap.put("total",total);
            Result res = Result.success(resMap);
            writer.write(JSON.toJSONString(res));
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
